import hxvid.Client;
import hxvid.Commands;
import hxvid.Data;
import hxvid.IApplication;
import hxvid.events.ConnectionEvent;
import hxvid.events.StreamEvent;
import hxvid.Logger;
import hxvid.Server;
import neko.FileSystem;

class Application implements IApplication
{
	public var config:ApplicationConfig;
	var clientid:Int;
	var server:Server;
	var logger:Logger;
	public function new(serv:Server, conf:ApplicationConfig):Void
	{
		clientid = 0;
		server = serv;
		config = conf;
		//server.addEventListener(ServerEvent.CLIENT_CONNECT, onBeforeConnect);
		logger = new Logger(config.logFile);
		logger.id = "video/Application";
		logger.info("started");
	}
	//function onBeforeConnect(event:ServerEvent):Void
	public function onConnect(client:Client, obj:ConnectionObject, ?arg:Dynamic=null):Void
	{
		//this happens before "NetConnection.Connect.Success" is sent back to the client
		//var client = event.data.client;
		//var connectionObject = event.data.obj;
		//var argument = event.data.arg;
		
		client.addEventListener(ConnectionEvent.CONNECTED, onConnected);
		client.addCommands([["getVideos", this.onGetVideos, T.Null ]]);
		client.id = clientid++;
		client.name = "guest" + client.id;
		logger.info("onConnect client:"+ cstring(client));
		client.accept();//client.reject("some reason"); // to send "NetConnection.Connect.Rejected" to the client
	}
	function onConnected(event:ConnectionEvent):Void
	{
		//this happens after the "NetConnection.Connect.Success" is sent back to the client
		var client = event.target;
		client.addEventListener(StreamEvent.STREAM_CREATE, onStreamCreate);
		client.addEventListener(StreamEvent.STREAM_PLAY, onStreamPlay);
		client.addEventListener(ConnectionEvent.DISCONNECTED, onDisconnected);
		logger.info("onConnected by client:" + cstring(client)); 
	}
	function onStreamCreate(event:StreamEvent):Void
	{
		var client = event.target;
		logger.info("onConnected client " + cstring(client) + " has received NetConnection.Connect.Success)");
	}
	function onStreamPlay(event:StreamEvent):Void
	{
		var client = event.target;
		var data = event.data;
		logger.info("onStreamPlay by client " + cstring(client)+", file:"+data.file);
	}
	function onDisconnected(event:ConnectionEvent):Void
	{
		var client  = event.target;
		logger.info("onDisconnected by client "+ cstring(client));
	}
	function onGetVideos(cinf:CommandInfos, _ ):Void
	{
		var client:Client = cinf.c;
		logger.info("onGetVideos requested by client "+ cstring(client));

		var appsDir = server.config.appsDir;
		var appName = server.getApplicationName(this);
		var streamsDir = config.streamsDir;
		var path = neko.Sys.getCwd() + appsDir + "/" + appName + "/" + streamsDir;
		
		var videoInfos = new Array();
		if(neko.FileSystem.isDirectory(path))
			for(f in neko.FileSystem.readDirectory(path) )
				if( (StringTools.endsWith(f, ".flv")||StringTools.endsWith(f, ".f4v")||StringTools.endsWith(f, ".mp4") ) && !neko.FileSystem.isDirectory(path+"/"+f))
					videoInfos.push({name:f, stat:neko.FileSystem.stat(path+"/"+f)});
		client.sendResult(cinf, videoInfos);
	}
	function cstring(client:Client)
	{
		return "id:" + client.id + ", ip:" + client.ip + ", name:" + client.name;
	}
}